package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.user.common.R;
import com.example.user.mapper.StockMapper;
import com.example.user.entity.Stock;
import com.example.user.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author JFeng
 * @Date 2024/5/21
 */

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
    @Autowired
    private StockMapper stockMapper;


    @Override
    public R<Stock> reduce(Integer productId) {
        R<Stock> r = new R<>();
        LambdaQueryWrapper<Stock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stock::getProductId, productId);
        Stock stock = stockMapper.selectOne(queryWrapper);
        stock.setQuantity(stock.getQuantity() - 1);
        int num = stockMapper.updateById(stock);
        r = num == 1 ? r.success(stock, "操作成功") : r.error(stock, "操作失败");
        return r;
    }
}
